import { Sticky, NavBar, Overlay, Notify } from "vant";
import PublicNullData from "@/components/PublicNullData/PublicNullData.vue";
import SceneListDetail from "@/views/list/_components/sceneListDetail/sceneListDetail.vue";
import PageSkeleton from "@/views/list/_components/skeleton/skeleton.vue";
import { lot_rule_page_api, lot_rule_onOffSingle_api } from "@/api/lot.js";
import { getLocalStorage } from "@/tools/index.js";
export default {
  name: "sceneList",
  components: {
    [Sticky.name]: Sticky,
    [NavBar.name]: NavBar,
    [Overlay.name]: Overlay,
    PublicNullData,
    PageSkeleton,
    SceneListDetail,
  },
  data() {
    return {
      // 弹窗对象
      popupObj: {
        show: false,
        overlay: true,
      },
      // 列表对象
      listObj: {
        isSkeleton: true,
        list: [],
        loading: false,
        finished: false,
      },
      // 查询入参
      queryCondition: {
        orgId: getLocalStorage("loginInfo").orgId,
        // 规则名称
        ruleName: "",
        // 群组id
        ruleGroupId: "",
        // 当前页
        page: 1,
        // 总页数
        pageTotal: 0,
        // 每页条目数
        rows: 10,
        // 规则id
        ruleId: "",
        // 指令
        operation: "",
      },
    };
  },
  created() {},
  mounted() {
    this.lot_rule_page_api();
  },
  methods: {
    // 规则一键启停
    lot_rule_onOffSingle_api() {
      let data = {
        ruleId: this.queryCondition.ruleId,
        operation: this.queryCondition.operation,
      };
      console.info(
        "lot_rule_onOffSingle_api规则一键启停入参" + JSON.stringify(data)
      );
      lot_rule_onOffSingle_api(data)
        .then((res) => {
          console.info(
            "lot_rule_onOffSingle_api规则一键启停出参" + JSON.stringify(res)
          );
          if (res.status == 200) {
            Notify({ type: "success", message: "一键启停成功" });
          } else {
            Notify({ type: "danger", message: res.msg });
          }
          // 规则分页查询
          this.lot_rule_page_api();
        })
        .catch((err) => {
          Notify({ type: "danger", message: "规则启停失败" });
        });
    },
    // 规则分页查询
    lot_rule_page_api() {
      let data = {
        ruleName: this.queryCondition.ruleName,
        groupId: this.queryCondition.groupId,
        page: this.queryCondition.page,
        rows: this.queryCondition.rows,
      };
      console.info(
        "lot_rule_page_api规则分页查询入参：" + JSON.stringify(data)
      );
      lot_rule_page_api(data)
        .then((res) => {
          console.info(
            "lot_rule_page_api规则分页查询出参：" + JSON.stringify(res)
          );
          if (res.status == 200) {
            this.queryCondition.page++;
            this.listObj.isSkeleton = false;
            this.queryCondition.pageTotal = Math.ceil(res.total / 10);
            res.obj.forEach((item) => {
              if (item.ruleStatus == 1) {
                item.status = true;
              } else {
                item.status = false;
              }
            });
            this.listObj.list = res.obj;
          } else {
            Notify({ type: "danger", message: res.msg });
          }
        })
        .catch((err) => {});
    },

    onLoad() {
      // 数据全部加载完成
      if (this.queryCondition.page >= this.queryCondition.pageTotal) {
        this.listObj.finished = true;
      } else {
        this.lot_rule_page_api();
        // 加载状态结束
        this.listObj.loading = false;
      }
    },
    // switch监听
    switchEmitFn(obj) {
      this.queryCondition.ruleId = obj.id;
      this.queryCondition.operation = obj.value;
      this.queryCondition.page = 1;
      this.lot_rule_onOffSingle_api();
    },
  },
};
